package cn.meebook.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.meebook.domain.Book;
import cn.meebook.domain.DomainBookCircleNumber;
import cn.meebook.domain.BookCircleRespository;
import cn.meebook.domain.BookDomain;
import cn.meebook.domain.BookDomainRepository;
import cn.meebook.domain.BookInstance;
import cn.meebook.domain.BookInstanceRepository;
import cn.meebook.domain.BookRepository;
import cn.meebook.domain.Circle;
import cn.meebook.domain.CircleRepository;
import cn.meebook.domain.Domain;
import cn.meebook.domain.DomainRepository;
import cn.meebook.domain.Meeker;
import cn.meebook.domain.MeekerRepository;

@Service
public class BookService {
	
	@Autowired
	private BookRepository bookRepository;
	
	@Autowired
	private BookInstanceRepository bookInstanceRepository;
	
	@Autowired
	private DomainRepository domainRepository;
	
	@Autowired
	private BookDomainRepository bookDomainRepository;
	
	@Autowired
	private BookCircleRespository bookCircleRespository;
	
	@Autowired
	private MeekerRepository meekerRepository;
	
	@Autowired
	private CircleRepository circleRepository;
	
	public List<DomainBookCircleNumber> getBookCircleNumbersInDomain(Domain bookDomain) {
        List<BookDomain> bookDomains = bookDomainRepository.findByDomain(bookDomain);
        List<DomainBookCircleNumber> dbcns = bookCircleRespository.listBookCircleNumber(bookDomains);
        return dbcns;
	}
	
	public List<Domain> getAllDomains() {
		return domainRepository.findAll();
	}
	
	public void test() {
		Book book = new Book("9787513300711", "失控", "[美] Kevin.Kery");
		Meeker wangfan = new Meeker("王帆", "rediscover");
		Meeker yahong = new Meeker("吴亚鸿", "flying hope");
		
		bookRepository.save(book);
		meekerRepository.saveAndFlush(wangfan);
		meekerRepository.saveAndFlush(yahong);
		
		Circle circle = new Circle();
		circle.setCreator(wangfan);
		circle.setName("哈士奇书吧");
		circle.getMembers().add(wangfan);
		circle.getMembers().add(yahong);
		circleRepository.saveAndFlush(circle);
		
		BookInstance shikong1 = new BookInstance(book, wangfan);
		BookInstance shikong2 = new BookInstance(book, yahong);
		bookInstanceRepository.save(shikong1);
		bookInstanceRepository.save(shikong2);
	}

}
